package org.jeecg.modules.mall.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.mall.entity.MallPosTableOrderGoods;
import org.jeecg.modules.mall.entity.MallPosTableOrderGoodsDetail;
import org.jeecg.modules.mall.service.IMallPosTableOrderGoodsDetailService;
import org.jeecg.modules.mall.service.IMallPosTableOrderGoodsService;
import org.jeecg.modules.pos.entity.PosRegion;
import org.jeecg.modules.pos.entity.PosTable;
import org.jeecg.modules.pos.service.IPosRegionService;
import org.jeecg.modules.pos.service.IPosTableService;
import org.jeecg.modules.rooms.service.CesGoodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

 /**
 * @Description: mall_pos_table_order_goods
 * @Author: jeecg-boot
 * @Date:   2023-05-17
 * @Version: V1.0
 */
@Api(tags="mall_pos_table_order_goods")
@RestController
@RequestMapping("/mall/mallPosTableOrderGoods")
@Slf4j
public class MallPosTableOrderGoodsController extends JeecgController<MallPosTableOrderGoods, IMallPosTableOrderGoodsService> {
	@Autowired
	private IMallPosTableOrderGoodsService mallPosTableOrderGoodsService;
@Resource
private IPosTableService posTableService;
@Resource
private IPosRegionService posRegionService;
@Resource
private IMallPosTableOrderGoodsDetailService mallPosTableOrderGoodsDetailService;
@Resource
private CesGoodsServiceImpl cesGoodsService;
	/**
	 * 分页列表查询
	 *
	 * @param mallPosTableOrderGoods
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "mall_pos_table_order_goods-分页列表查询")
	@ApiOperation(value="mall_pos_table_order_goods-分页列表查询", notes="mall_pos_table_order_goods-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<MallPosTableOrderGoods>> queryPageList(MallPosTableOrderGoods mallPosTableOrderGoods,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
															   @RequestParam(name="keyword",required = false) String keyword,
															   @RequestParam(name="startTime",required = false) String startTime,
															   @RequestParam(name="endTime",required = false) String endTime,
								   HttpServletRequest req) {
		LambdaQueryWrapper<MallPosTableOrderGoods> queryWrapper = QueryGenerator.initQueryWrapper(mallPosTableOrderGoods, req.getParameterMap()).lambda();
		if (ObjectUtils.isNotEmpty(startTime)) {
			queryWrapper.ge(MallPosTableOrderGoods::getCreateTime, DateUtil.parseDate(startTime));
		}
		if (ObjectUtils.isNotEmpty(endTime)) {
			queryWrapper.le(MallPosTableOrderGoods::getCreateTime, DateUtil.parseDate(endTime));
		}
		if (ObjectUtils.isNotEmpty(keyword)) {
			LambdaQueryWrapper<PosTable> lambdaQueryWrapper = new LambdaQueryWrapper<>();
			lambdaQueryWrapper.eq(PosTable::getName, keyword);
			List<PosTable> posTables = posTableService.list(lambdaQueryWrapper);
			queryWrapper.and(t -> {
				t.or().like(MallPosTableOrderGoods::getCode, "%" + keyword + "%");
				if (posTables != null) {
					posTables.forEach(p -> {
						t.or().eq(MallPosTableOrderGoods::getPosTableId, p.getId());
					});
				}
			});
		}
		Page<MallPosTableOrderGoods> page = new Page<MallPosTableOrderGoods>(pageNo, pageSize);
		IPage<MallPosTableOrderGoods> pageList = mallPosTableOrderGoodsService.page(page, queryWrapper);
		pageList.getRecords().forEach(t -> {
			PosTable posTable = posTableService.getById(t.getPosTableId());
			if (posTable != null) {
				t.setTableNo(posTable.getName());
				PosRegion posRegion = posRegionService.getById(posTable.getPosRegionId());
				if (posRegion != null) {
					t.setRegionName(posRegion.getName());
				}
			}
			switch (t.getStatus()) {
				case 1:
					t.setStatusName("待接单");
					break;
				case 2:
					t.setStatusName("已接单");
					break;
				case 3:
					t.setStatusName("已完成");
					break;
				case 4:
					t.setStatusName("已拒绝");
					break;
				case 5:
					t.setStatusName("已取消");
					break;
			}
			LambdaQueryWrapper<MallPosTableOrderGoodsDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
			lambdaQueryWrapper.eq(MallPosTableOrderGoodsDetail::getOrderCode, t.getCode());
			List<MallPosTableOrderGoodsDetail> list=mallPosTableOrderGoodsDetailService.list(lambdaQueryWrapper);
			if(ObjectUtils.isNotEmpty(list)){
				list.forEach(i->{
					i.setGoodsInfo(cesGoodsService.getById(i.getGoodsId()));
				});
			}
			t.setDetailList(list);
		});
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param mallPosTableOrderGoods
	 * @return
	 */
	@AutoLog(value = "mall_pos_table_order_goods-添加")
	@ApiOperation(value="mall_pos_table_order_goods-添加", notes="mall_pos_table_order_goods-添加")
	//@RequiresPermissions("mall:mall_pos_table_order_goods:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody MallPosTableOrderGoods mallPosTableOrderGoods) {
		mallPosTableOrderGoodsService.save(mallPosTableOrderGoods);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param mallPosTableOrderGoods
	 * @return
	 */
	@AutoLog(value = "mall_pos_table_order_goods-编辑")
	@ApiOperation(value="mall_pos_table_order_goods-编辑", notes="mall_pos_table_order_goods-编辑")
	//@RequiresPermissions("mall:mall_pos_table_order_goods:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody MallPosTableOrderGoods mallPosTableOrderGoods) {
		mallPosTableOrderGoodsService.updateById(mallPosTableOrderGoods);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "mall_pos_table_order_goods-通过id删除")
	@ApiOperation(value="mall_pos_table_order_goods-通过id删除", notes="mall_pos_table_order_goods-通过id删除")
	//@RequiresPermissions("mall:mall_pos_table_order_goods:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		mallPosTableOrderGoodsService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "mall_pos_table_order_goods-批量删除")
	@ApiOperation(value="mall_pos_table_order_goods-批量删除", notes="mall_pos_table_order_goods-批量删除")
	//@RequiresPermissions("mall:mall_pos_table_order_goods:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.mallPosTableOrderGoodsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "mall_pos_table_order_goods-通过id查询")
	@ApiOperation(value="mall_pos_table_order_goods-通过id查询", notes="mall_pos_table_order_goods-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<MallPosTableOrderGoods> queryById(@RequestParam(name="id",required=true) String id) {
		MallPosTableOrderGoods mallPosTableOrderGoods = mallPosTableOrderGoodsService.getById(id);
		if(mallPosTableOrderGoods==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(mallPosTableOrderGoods);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param mallPosTableOrderGoods
    */
    //@RequiresPermissions("mall:mall_pos_table_order_goods:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MallPosTableOrderGoods mallPosTableOrderGoods) {
        return super.exportXls(request, mallPosTableOrderGoods, MallPosTableOrderGoods.class, "mall_pos_table_order_goods");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("mall:mall_pos_table_order_goods:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MallPosTableOrderGoods.class);
    }

}
